package com.ruoyi.tester.executor.http;

import com.ejlchina.data.ListMap;
import com.ejlchina.okhttps.HTTP;
import com.ejlchina.okhttps.HttpResult;
import com.ejlchina.okhttps.SHttpTask;
import org.json.JSONObject;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @author SongJun
 * @description TODO:
 * @date 2022/5/30 11:33
 */
public class OkhttpsUtil implements HttpUtils {
    private SHttpTask buildSHttpTask(String url, String contentType, Map<String, String> headers, String queryParam, String bodyParam, String pathParam){
        HTTP http = HTTP.builder().build();
        SHttpTask task = http.sync(url);
        if(StringUtils.hasLength(contentType)){
            task.bodyType(contentType);
        }
        if(!CollectionUtils.isEmpty(headers)){
            task.addHeader(headers);
        }
        if(StringUtils.hasLength(queryParam)){
            JSONObject queryJson = new JSONObject(queryParam);
            for (String key : queryJson.keySet()) {
                task.addUrlPara(key, queryJson.get(key));
            }
        }
        if(StringUtils.hasLength(bodyParam)){
            task.setBodyPara(new JSONObject(bodyParam));
        }
        if(StringUtils.hasLength(pathParam)){
            JSONObject pathJson = new JSONObject(pathParam);
            for (String key : pathJson.keySet()) {
                task.addPathPara(key, pathJson.get(key));
            }
        }
        return task;
    }

    private Map<String, String> getResponseHeader(HttpResult httpResult){
        ListMap<String> stringListMap = httpResult.allHeaders();
        Map<String, String> respHeader = new HashMap<>();
        for(String key : stringListMap.keySet()){
            String v = stringListMap.get(key);
//            List<String> list = stringListMap.list(key);
            respHeader.put(key, v);
        }
        return respHeader;
    }

    @Override
    public XzfHttpResult get(String url, String contentType, Map<String, String> headers, String queryParam, String pathParam) {
        SHttpTask task = buildSHttpTask(url, contentType, headers, queryParam, null, pathParam);
        HttpResult httpResult = task.get();
        Map<String, String> responseHeader = getResponseHeader(httpResult);
        XzfHttpResult result = new XzfHttpResult(true, httpResult.getStatus(), httpResult.getBody().toString(), responseHeader);
        return result;
    }

    @Override
    public XzfHttpResult head(String url, String contentType, Map<String, String> headers, String queryParam, String pathParam) {
        return null;
    }

    @Override
    public XzfHttpResult post(String url, String contentType, Map<String, String> headers, String queryParam, String bodyParam, String pathParam) {
        SHttpTask task = buildSHttpTask(url, contentType, headers, queryParam, bodyParam, pathParam);
        HttpResult httpResult = task.post();
        Map<String, String> responseHeader = getResponseHeader(httpResult);
        XzfHttpResult result = new XzfHttpResult(true, httpResult.getStatus(), httpResult.getBody().toString(), responseHeader);
        return result;
    }

    @Override
    public XzfHttpResult put(String url, String contentType, Map<String, String> headers, String queryParam, String bodyParam, String pathParam) {
        return null;
    }

    @Override
    public XzfHttpResult delete(String url, String contentType, Map<String, String> headers, String queryParam, String bodyParam, String pathParam) {
        return null;
    }

    @Override
    public XzfHttpResult patch(String url, String contentType, Map<String, String> headers, String queryParam, String bodyParam, String pathParam) {
        return null;
    }

    @Override
    public XzfHttpResult options(String url, String contentType, Map<String, String> headers, String queryParam, String bodyParam, String pathParam) {
        return null;
    }

    @Override
    public XzfHttpResult trace(String url, String contentType, Map<String, String> headers, String queryParam, String bodyParam, String pathParam) {
        return null;
    }
}
